package com.ablife.limiter.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class ReturnService {
    @Autowired
    JedisPool jedisPool;

    public ReturnService() {
    }

    public String getReturnString(String channel, String tradeCode, boolean flag) {
        Jedis jedis = this.jedisPool.getResource();
        channel = channel.toUpperCase();
        tradeCode = tradeCode.toUpperCase();
        String key;
        String value;
        String returnStr;
        if (flag) {
            key = channel + "|" + tradeCode + "|" + "SUCCESS";
            value = jedis.get(key);
            if (jedis.exists(key) && !StringUtils.isEmpty(value)) {
                returnStr = "{\"code\":\"1\",\"description\":\"" + value + "\"}";
            } else {
                returnStr = "{\"code\":\"1\",\"description\":\"访问成功\"}";
            }
        } else {
            key = channel + "|" + tradeCode + "|" + "FAILURE";
            value = jedis.get(key);
            if (jedis.exists(key) && !StringUtils.isEmpty(value)) {
                returnStr = "{\"code\":\"0\",\"description\":\"" + value + "\"}";
            } else {
                returnStr = "{\"code\":\"0\",\"description\":\"系统繁忙\"}";
            }
        }

        jedis.close();
        return returnStr;
    }
}